<template>
  <div class="global">
    <div class="divItem">
      <!--      <button style="margin: 3px" @click="onBtnClick('dockLeft')">停靠左边</button>-->
      <!--      <button style="margin: 3px" @click="onBtnClick('dockRight')">停靠右边</button>-->
      <!--      <button style="margin: 3px" @click="onBtnClick('hideTaskPane')">隐藏TaskPane</button>-->
      <!--      <button style="margin: 3px" @click="onBtnClick('addString')">文档开头添加字符串</button>-->
      <!--      <button style="margin: 3px" @click="onDocNameClick()">取文件名</button>-->
      <button style="margin: 3px" @click="testEbsilon()">测试Ebsilon是否可用</button>
      <button style="margin: 3px" @click="chooseModelFile()">选择模型文件</button>
      <button style="margin: 3px" @click="initCurve()">初始化</button>
      <button style="margin: 3px" @click="generateTCurveRegion()">生成环境温度修正数据区域</button>
      <button style="margin: 3px" @click="generateTCurve()">生成环境温度修正曲线</button>

      <button style="margin: 3px" @click="smoothCurve()">平滑曲线</button>
      <button style="margin: 3px" @click="insertChart()">插入图表</button>

    </div>
    {{ docName }}
    {{ available }}
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import axios from 'axios'
import taskPane from './ts/taskPane'
import EtChart = Et.EtChart
import EtChartArea = Et.EtChartArea
import { etUtils, utils } from 'yklib'
import EtXlSheetType = Et.EtXlSheetType
import EtWorksheet = Et.EtWorksheet
import EtRange = Et.EtRange
import Dialog from '@/components/Dialog.vue'
import { pandas as pd } from 'yklib'
import Util from '@/components/ts/util'

const available = ref('')
// const name = 'TaskPane'
const docName = ref('')


const onBtnClick = (id: string) => {
  return taskPane.onBtnClick(id)
}

const onDocNameClick = () => {
  docName.value = taskPane.onBtnClick('getDocName')
}

/**
 * 根据选择的区域插入图标
 */
const insertChart = () => {
  let sheet = window.Application.ActiveWorkbook.ActiveSheet
  let shapes = sheet.Shapes
  let count1 = shapes.Count
  let selection = window.Application.Selection as EtRange
  if (selection.Count == 1) {
    alert('选择数据区域后，再插入图表')
    return
  }
  console.log(selection.Count)
  shapes.AddChart2(-1, 65, 0, 0, 400, 300)  // https://learn.microsoft.com/zh-cn/office/vba/api/excel.shapes.addchart2
  let chart = getLastAddedChart(sheet)
  // chart.ChartTitle.Text = "测试" 有些类型的图标貌似没有标题
  if (chart) {
    chart.SetSourceData(selection, 1)
    etUtils.ChartSetLocation(chart, 100, 200)
  }
}

/**
 * 初始化，设置热平衡图参数
 */
const initCurve = () => {
  let sheet = etUtils.create_new_sheet(window.Application, '建模')
  // etUtils.
  let df = new pd.DataFrame([
    ['请手动设置天然气组分', '', '', '', ''],
    ['大气压力', 'spec', 'P_MP_AIR.MEASM', '大气压力', 'kPa'],
    ['环境温度', 'spec', 'Input_BV_GT.T', '燃机入口环境温度', '℃'],
    ['环境湿度', 'spec', 'Input_MP_GT.MEASM', '燃机入口空气湿度', '-'],
    ['压气机入口压力', 'spec', 'Input_BV_GT.P', '压气机入口压力', 'kPa'],
    ['天然气压力', 'spec', 'P_FGH_gas_in.MEASM', '天然气压力', 'MPa'],
    ['天然气温度', 'spec', 'T_FGH_gas_in.MEASM', '天然气温度', '°C'],
    ['天然气流量', 'spec', 'F_gas_in.MEASM', '天然气流量', 'kg/h'],
    ['FGH出口燃气压力', 'spec', 'P_FGH_gas_out.MEASM', 'FGH出口燃气压力', 'MPa'],
    ['FGH出口燃气温度', 'spec', 'T_FGH_gas_out.MEASM', 'FGH出口燃气温度', '℃'],
    ['燃机发电机功率', 'spec', 'Power_GT.MEASM', '燃机发电机功率', 'MW'],
    ['汽机发电机功率', 'spec', 'Power_ST.MEASM', '汽机发电机功率', 'MW'],
    ['燃机排气压力', 'spec', 'P_MP_GT_out.MEASM', '燃机排气压力', 'MPa'],
    ['燃机排气温度', 'spec', 'T_MP_GT_out.MEASM', '燃机排气温度', '℃'],
    ['余热锅炉入口烟气压力', 'spec', 'P_HRSG_in.MEASM', '余热锅炉入口烟气压力', 'MPa'],
    ['余热锅炉入口烟气温度', 'spec', 'T_HRSG_in.MEASM', '余热锅炉入口烟气温度', '℃'],
    ['余热锅炉入口烟气流量', 'spec', 'F_HRSG_in.MEASM', '余热锅炉入口烟气流量', 't/h'],
    ['余热锅炉出口烟气压力', 'spec', 'P_HRSG_out.MEASM', '余热锅炉出口烟气压力', 'MPa'],
    ['余热锅炉出口烟气温度', 'spec', 'T_HRSG_out.MEASM', '余热锅炉出口烟气温度', '℃'],
    ['高压主汽压力(炉侧)', 'spec', 'P_HMS_SG.MEASM', '', 'MPa'],
    ['高压主汽温度(炉侧)', 'spec', 'T_HMS_SG.MEASM', '', '℃'],
    ['高压主汽流量(炉侧)', 'spec', 'F_HMS_SG.MEASM', '', 't/h'],
    ['高压主汽压力(机侧)', 'spec', 'P_HMS_ST.MEASM', '', 'MPa'],
    ['高压主汽温度(机侧)', 'spec', 'T_HMS_ST.MEASM', '', '℃'],
    ['高压主汽流量(机侧)', 'spec', 'F_HMS_ST.MEASM', '', 't/h'],
    ['高压缸漏汽流量', 'spec', 'F_HP_ST.MEASM', '', 't/h'],
    ['高排压力(炉侧)', 'spec', 'P_HP_SG_out', '', 'MPa'],
    ['高排温度(炉侧)', 'spec', 'T_HP_SG_out', '', '℃'],
    ['高排压力(机侧)', 'spec', 'P_HP_ST_out', '', 'MPa'],
    ['高排温度(机侧)', 'spec', 'T_HP_ST_out', '', '℃'],

    ['中压主汽压力(炉侧)', 'spec', 'P_MMS_SG.MEASM', '', 'MPa'],
    ['中压主汽温度(炉侧)', 'spec', 'T_MMS_SG.MEASM', '', '℃'],
    ['中压主汽流量(炉侧)', 'spec', 'F_MMS_SG.MEASM', '', 't/h'],
    ['中压主汽压力(机侧)', 'spec', 'P_MMS_ST.MEASM', '', 'MPa'],
    ['中压主汽温度(机侧)', 'spec', 'T_MMS_ST.MEASM', '', '℃'],
    ['中压主汽流量(机侧)', 'spec', 'F_MMS_ST.MEASM', '', 't/h'],
    ['中压缸漏汽流量', 'spec', 'F_IP_ST.MEASM', '', 't/h'],
    ['中排压力', 'spec', 'P_MP_out', '', 'MPa'],
    ['中排温度', 'spec', 'T_MP_out', '', '℃'],
    ['低压主汽压力(炉侧)', 'spec', 'P_LMS_SG.MEASM', '', 'MPa'],
    ['低压主汽温度(炉侧)', 'spec', 'T_LMS_SG.MEASM', '', '℃'],
    ['低压主汽流量(炉侧)', 'spec', 'F_LMS_SG.MEASM', '', 't/h'],
    ['低压缸进汽压力(机侧)', 'spec', 'P_LMS_ST.MEASM', '', 'MPa'],
    ['低压缸进汽温度(机侧)', 'spec', 'T_LMS_ST.MEASM', '', '℃'],
    ['低压缸进汽流量(机侧)', 'spec', 'F_LMS_ST.MEASM', '', 't/h'],
    ['低压缸漏汽流量', 'spec', 'F_LP_ST.MEASM', '', 't/h'],
    ['低压缸排汽压力', 'spec', 'P_LP_out.MEASM', '', 'MPa'],
    ['低压缸排汽焓', 'spec', 'H_LP_out_MEASM', '', 'kJ/kg'],


    ['汽机发电机效率', 'spec', 'Gen_ST.ETAGN', '汽机发电机效率', '-']
  ])
  df.toEtRange(1, 1)
}

const getLastAddedChart = (sheet: EtWorksheet): EtChart => {
  let chartObject = sheet.ChartObjects(sheet.Shapes.Count) as unknown | void as { Chart: EtChart }
  return chartObject.Chart
}

const testEbsilon = () => {
  axios.get('http://localhost:20011/?Action=EbsilonAvailable').then((res) => {
    if (res) {
      available.value = res.data
    } else {
      available.value = res.data
    }
  })
}

const chooseModelFile = () => {
  let filePath = etUtils.FileDialogSelectFile(window.Application)
  if (filePath === '') {
    // 点击了取消按钮
  } else {
    // 选择了模型文件
    etUtils.set_cell_value(window.Application, 1, 1, '模型文件')
    etUtils.set_cell_value(window.Application, 1, 2, filePath)
  }
}

/**
 * 创建环境温度修正曲线
 */
const generateTCurve = () => {
  // 在Excel中创建环境温度修正曲线的数据区域
  let df = new pd.DataFrame([
    ['环境温度', 15, 17.5, 20, 22.5, 25, 27.5, 30, 32.5, 35],
    ['输出功率', 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ['热耗率', 0, 0, 0, 0, 0, 0, 0, 0, 0]
  ])
  df.toEtRange('A3') // 将pd的数据写入wps et的单元格内，从A3开始


}
const generateTCurveRegion = () => {
  // 在Excel中创建环境温度修正曲线的数据区域
  let df = new pd.DataFrame([
    ['环境温度', 15, 17.5, 20, 22.5, 25, 27.5, 30, 32.5, 35],
    ['输出功率', 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ['热耗率', 0, 0, 0, 0, 0, 0, 0, 0, 0]
  ])
  df.toEtRange(7, 1) // 将pd的数据写入wps et的单元格内，从A3开始
}

const smoothCurve = () => {
  Util.showDialog('/dialog', '平滑曲线设置', 950, 440)
}

onMounted(() => {
})
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.global {
  font-size: 15px;
  min-height: 95%;
}

.divItem {
  margin-left: 5px;
  margin-bottom: 18px;
  font-size: 15px;
  word-wrap: break-word;
}
</style>
